<template>
  <div class="login-box">
    <el-form ref="form" :model="form" label-width="80px" :rules="rules">
      <h3>欢迎登录</h3>
      <el-form-item label="用户名" prop="username">
        <el-input v-model="form.username" placeholder="请输入用户名"></el-input>
      </el-form-item>

      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="form.password" placeholder="请输入密码"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="login('form')">登录</el-button>
      </el-form-item>
    </el-form>
  </div>

</template>

<script>
import Qs from 'qs'

export default {
  name: "Login",
  data(){
    return{
      form:{
        username:'',
        password:''
      },
      rules:{
        username: [
          {required:true,message:'请输入用户名',trigger:'blur'},
          {min:1,max:10,message: '长度在1 到 10个字符',trigger: 'blur'}
        ],
        password: [
          {required:true,message:'请输入密码',trigger:'blur'},
          {min:1,max:20,message: '长度在1 到 20个字符',trigger: 'blur'}
        ]
      },
    }
  },
  methods:{
    login(forName){
      this.$refs[forName].validate((valid)=>{
        let vm = this;
        if (valid){
          this.axios({
            url:'http://localhost:9090/user/login',
            method:'POST',
            data:{
              username:vm.form.username,
              password:vm.form.password
            },
          }).then(function (resp) {
            if (resp.data.code == 0){
              vm.$message({
                message:resp.data.msg,
                type:'success'
              });
              vm.$store.commit('updateUser',resp.data.data);
              setTimeout(function () {
                vm.$router.push("/cardList")
              },1000);
            }else {
              vm.$message({
                message:resp.data.msg,
                type:'error'
              });
            }
          });
        }else{
          this.$message.error('输入格式错误');
          return false;
        }
      });
    }
  }

}
</script>

<style scoped>
.login-box{
  width: 500px;
  height: 300px;
  border: 1px solid #DCDFE6;
  margin: 150px auto;
  padding: 20px 50px;
  border-radius: 20px;
  box-shadow: 0px 0px 20px #DCDFE6;
}
</style>
